<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="65px">
        <el-form-item label="工厂">
          <el-select
            @keyup.enter="search"
            v-model="searchParam.plantId"
            placeholder="请选择工厂"
            clearable
            @change="selectPlant"
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心">
          <el-select
            @keyup.enter="search"
            v-model="searchParam.workCenterId"
            placeholder="请选择工作中心"
            clearable
            filterable
          >
            <el-option
              v-for="item in workShopList"
              :key="item.id"
              :label="`${item.workshopCode}-${item.workshopName}`"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作年份">
          <el-select
            @keyup.enter="search"
            v-model="searchParam.planYear"
            placeholder="请选择工作年份"
            clearable
            @change="selectYear"
          >
            <el-option
              v-for="item in yearOptions"
              :key="item"
              :value="item"
              :label="item"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作周">
          <el-select
            @keyup.enter="search"
            v-model="searchParam.planWeek"
            placeholder="请选择工作周"
            clearable
          >
            <el-option
              v-for="item in monthOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="生产批次">
          <el-input
            v-model="searchParam.planBatch"
            placeholder="请输入生产批次"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item>
          <!-- <el-popconfirm
            title="请确认当前数据是否已被保存，切换后未保存的数据会被清空，是否切换?"
            confirm-button-text="切换"
            cancel-button-text="取消"
            :width="300"
            @confirm="startSearch"
          >
            <template #reference>
              <el-button v-show="tableData && tableData.length" type="primary"
                ><i-ep-search />切换</el-button
              >
            </template>
          </el-popconfirm>
          <el-button
            v-show="!tableData || !tableData.length"
            type="primary"
            @click="startSearch"
            ><i-ep-search />切换</el-button
          > -->
          <el-button type="primary" @click="startSearch"
            ><i-ep-search />切换</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <!-- <el-button
          type="primary"
          :loading="saveLoading"
          @click="validIsSearched('保存')"
          >保 存</el-button
        > -->
        <el-button type="primary" @click="addPlan">批量添加</el-button>
        <!-- <el-button type="primary" @click="submits">批量确定</el-button> -->
        <!-- <el-button @click="weekBatchImport"
          ><template #icon><i-ep-upload /></template>导入周计划</el-button
        >
        <el-button @click="dailyBatchImport"
          ><template #icon><i-ep-upload /></template>导入日计划</el-button
        >
        <el-button class="ml-3" @click="downLoadExcel">
          <template #icon><i-ep-download /></template>导出</el-button
        >
        <ImportExcel ref="weekImportRef" />
        <ImportExcel ref="dailyImportRef" /> -->
      </template>

      <el-form
        ref="formRef"
        :model="tableData"
        :rules="rules"
        label-width="90px"
        style="width: 100%"
        class="require-form planTable"
      >
        <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          :span-method="(param) => objectSpanMethod(param, 13)"
          max-height="600"
          border
          :header-cell-style="{ backgroundColor: '#fff' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" fixed />
          <el-table-column
            label="物料号//工序-人数-版本号"
            min-width="200"
            prop="materialId"
            show-overflow-tooltip
            align="center"
            fixed
          >
            <template #default="{ $index, row }">
              <span>{{ `${row.descriptionCode}-${row.versionNo}` }}</span>
            </template>
          </el-table-column>
          <el-table-column label="卷料状态" width="100" align="center">
            <template #default="{ row, $index }">
              <!-- <svg-icon
                icon-class="material"
                class="text-[17px] !mr-[0px] cursor-pointer color-[#409eff]"
                @click="selectMaterials(row, $index)"
              /> -->
              <!-- <el-tag v-if="row.requestPlanList.length === 0" type="success"
                ><svg-icon
                  icon-class="yifenpei"
                  class="text-[14px] c-##67c23a"
                />已分配</el-tag
              > -->
              <el-tooltip
                v-if="row.requestPlanList && row.requestPlanList?.length !== 0"
                content="已分配"
                placement="bottom"
                effect="light"
              >
                <svg-icon
                  icon-class="yifenpei"
                  class="text-[20px] c-#67C23A !mr-0"
                />
              </el-tooltip>

              <el-tooltip
                v-else
                content="待分配"
                placement="bottom"
                effect="light"
              >
                <svg-icon
                  icon-class="daifenpei"
                  class="text-[20px] c-#E6A23C !mr-0"
                />
              </el-tooltip>

              <!-- <el-tag v-if="row.requestPlanList.length !== 0" type="warning"
                >待分配</el-tag
              > -->
            </template>
          </el-table-column>
          <el-table-column
            label="班产"
            prop="yield"
            show-overflow-tooltip
            header-align="center"
            align="right"
          />
          <el-table-column
            label="状态"
            prop="planState"
            align="center"
            min-width="100"
          >
            <template #default="scope">
              <el-tag v-show="scope.row.planState == 0" type="success">{{
                useDictionary("PlanStateList", scope.row.planState ?? "")
              }}</el-tag>
              <el-tag v-show="scope.row.planState != 0">{{
                useDictionary("PlanStateList", scope.row.planState ?? "")
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="计划数量"
            prop="planQuantity"
            show-overflow-tooltip
            min-width="100"
            header-align="center"
            align="right"
          >
            <template #default="{ $index, row }">
              <span>{{ row.planQuantity }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="卷料"
            prop="materialCode"
            show-overflow-tooltip
            min-width="180"
            header-align="center"
            align="right"
          >
            <template #default="{ $index, row }">
              <span
                >{{ row.rollMaterialCode
                }}{{ row.rollMaterialCode && row.rollMaterialName ? "-" : ""
                }}{{ row.rollMaterialName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            label="重量"
            prop="materialWeight"
            show-overflow-tooltip
            min-width="100"
            header-align="center"
            align="right"
          >
            <template #default="{ $index, row }">
              <span>{{ row.materialWeight }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="理论数量"
            prop="theoryQuantity"
            show-overflow-tooltip
            min-width="100"
            header-align="center"
            align="right"
          >
            <template #default="{ $index, row }">
              <span>{{ row.theoryQuantity }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="生产批次"
            prop="planBatch"
            show-overflow-tooltip
            min-width="100"
            align="center"
          >
            <template #default="{ $index, row }">
              <span>{{ row.planBatch }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="批次数量"
            prop="batchQuantity"
            show-overflow-tooltip
            min-width="100"
            header-align="center"
            align="right"
          >
            <template #default="{ $index, row }">
              <span>{{ row.batchQuantity }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="已完成数量"
            prop="planFinishQuantity"
            width="100"
            show-overflow-tooltip
            header-align="center"
            align="right"
          />

          <!-- <el-table-column label="卷料" align="center" fixed="right">
            <template #default="{ row, $index }">
              <svg-icon
                icon-class="material"
                class="text-[17px] !mr-[0px] cursor-pointer color-[#409eff]"
                @click="selectMaterials(row, $index)"
              />
            </template>
          </el-table-column> -->

          <!-- <el-table-column
            label="卷料产量"
            prop="defaultPlanQuantity"
            show-overflow-tooltip
            header-align="center"
            align="right"
            fixed="right"
          >
            <template #default="{ $index, row }">
              <span>{{ row.defaultPlanQuantity }}</span>
            </template>
          </el-table-column> -->
          <el-table-column
            label="卷料操作"
            align="center"
            fixed="right"
            width="160"
          >
            <template #default="{ row, $index }">
              <el-button
                type="primary"
                @click="selectMaterials(row, $index)"
                class="h-[28px]"
                >选择</el-button
              >
              <!-- <el-button
                v-if="row.planState == 0 && !row.isSureRequestPlan"
                class="text-[12px]"
                @click="cancelRequstPlan($index)"
                >取消确认</el-button
              > -->
            </template>
          </el-table-column>

          <!-- <el-table-column
            label="卷料操作"
            align="center"
            fixed="right"
            width="160"
          >
            <template #default="{ row, $index }">
              <el-button
                type="primary"
                @click="sureRequstPlan($index)"
                class="h-[28px]"
                >确 认</el-button
              > -->
          <!-- <el-button
                v-if="row.planState == 0 && !row.isSureRequestPlan"
                class="text-[12px]"
                @click="cancelRequstPlan($index)"
                >取消确认</el-button
              > -->
          <!-- </template>
          </el-table-column> -->

          <!-- <el-table-column
            label="未排数量"
            prop="unPlanQuantity"
            show-overflow-tooltip
            align="right"
          /> -->
          <!-- <el-table-column label="" width="60" align="center">
            <template #default="{ row, $index }">
              <span class="text-[13px]" v-if="($index + 1) % 2 !== 0"
                >计划</span
              >
              <span class="text-[13px]" v-else>实际</span>
            </template>
          </el-table-column>

          <el-table-column :label="`CW${searchParam.planWeek}`" align="center">
            <template #default="">
              <el-table-column
                v-for="(item, index) in currentWeekDetail"
                :key="index"
                :label="`${item.date} 周${ToChineseDate(item.week)}`"
                :label-class-name="`${item.isFree ? 'bg-free' : ''}`"
              >
                <el-table-column
                  v-for="(monthItem, monthIndex) in classCount"
                  :label="`${convertToChineseNumber(monthItem, 2)}班`"
                  :key="monthIndex"
                  :prop="`planQuantity_${index}_${monthIndex}`"
                  class-name="percent_cell"
                  header-align="center"
                  align="right"
                  :label-class-name="`${item.isFree ? 'bg-free' : ''}`"
                >
                  <template #default="{ row, $index }">
                    <div
                      v-if="($index + 1) % 2 !== 0"
                      @click="
                        open(
                          row,
                          $index,
                          index,
                          monthIndex,
                          row?.newOrders[index]?.newClasses[monthIndex]
                            ?.planQuantity
                        )
                      "
                    >
                      <span
                        :class="
                          row?.newOrders[index]?.newClasses[monthIndex]
                            ?.planState == 5
                            ? 'text-coolGray'
                            : ''
                        "
                      >
                        {{
                          row?.newOrders[index]?.newClasses[monthIndex]
                            ?.planQuantity ?? ""
                        }}
                      </span>
                    </div>
                    <div v-else class="bg-[#fef0f0]">
                      <span
                        :class="
                          row?.newOrders[index]?.newClasses[monthIndex]
                            ?.planState === 5
                            ? 'text-coolGray'
                            : ''
                        "
                      >
                        {{
                          row?.newOrders[index]?.newClasses[monthIndex]
                            ?.planFinishQuantity ?? ""
                        }}
                      </span>
                    </div>
                  </template>
                </el-table-column>
              </el-table-column>
            </template>
          </el-table-column> -->

          <!-- <el-table-column
            label="操作"
            width="120"
            align="center"
            fixed="right"
          >
            <template #default="{ row, $index }">
              <el-button
                type="danger"
                link
                class="text-[13px]"
                @click="deletePlan($index)"
                >删除</el-button
              >
              <el-button
                type="danger"
                link
                class="text-[13px]"
                @click="deletePlan($index)"
                >关闭</el-button
              >
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
    </el-card>
    <SelectMaterialPlanDialog
      v-model:visible="materialsSelect.visible"
      :data="materialsSelect.data"
      :allocationId="materialsSelect.allocationId"
      :eldIds="materialsSelect.eldIds"
      :readonly="materialsSelect.readonly"
      :planQuantity="materialsSelect.planQuantity"
      :isMultiple="true"
      :plantId="searchedParams?.plantId"
      :companyId="searchedCompanyId"
      :rowIndex="materialsSelect?.rowIndex"
      @echo-info="getMaterialsSelect"
    />
    <el-dialog
      :close-on-click-modal="false"
      v-model="dialogVisible"
      title="计划排产数量"
      width="450"
      align-center
      draggable
      class="planDialog"
      @close="clearRuleForm"
    >
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rulesPlan"
        label-width="auto"
        label-position="left"
        class="demo-ruleForm"
        status-icon
      >
        <div class="mb-[6px]">
          {{ `未排产数量：${ruleForm.unPlanQuantity}`
          }}{{ `班产数量：${ruleForm.yelid}` }}
          {{ `剩余班产数量：${ruleForm.noYelid}` }}
        </div>
        <el-form-item prop="quantity">
          <el-input
            v-model="ruleForm.quantity"
            placeholder="请输入计划排产数量"
            ref="inputRef"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="clearRuleForm">取消</el-button>
          <el-button type="primary" @click="submitPlanQuantity">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import SelectMaterialPlanDialog from "@/views/mes/weeklyProducePlan/components/select-material-plan-dialog.vue";
// import { getPlantsApi, getYearsApi, getWorkShopsApi } from "@/api/plant/index";
import {
  getPlantList,
  getFirstWorkShopList,
  getMesRouteGroupList,
  getWorkCenterDetail,
  getCurrentYield,
  getFreeDetails,
  getMonthList,
  getYearList,
  getMaterialList,
  getDateshiftCount,
} from "@/utils/commonData";
import {
  getWeeksInYear,
  getWeekNumber,
  week_date,
  getDaysOfCurrentWeek,
} from "@/utils/dateUtils";
import type { TableColumnCtx } from "element-plus";
import { convertToChineseNumber, validateNumber } from "@/utils/commonHooks";
import {
  getOneLineDate,
  WeekDetailType,
  SpanMethodProps,
  SearchType,
  resolveSearchData,
  resolveSaveData,
  ToChineseDate,
  getCurrentWeekDetail,
  objectSpanMethod,
  calculateLoadRate,
  getSummaries,
  mockData,
  getNameInfo,
  deepCopy,
} from "@/views/mes/weeklyProducePlan/index";

// import Big from "big.js";

defineOptions({
  name: "MESWEEKLYPRODUCEPLANSECOND",
  inheritAttrs: false,
});

const dialogVisible = ref(false);
const ruleForm: any = reactive({
  quantity: "",
  rowData: null,
  rowIndex: null,
  monthIndex: null,
  cellIndex: null,
  unPlanQuantity: null,
  yelid: "",
  noYelid: "",
});

// 停用过滤
const selected1 = (row) => {
  return true; //可勾选
};

// 进入弹窗聚焦
// 进入页面聚焦
const inputRef = ref();
const readonly = ref(true);
const inputAutoFocus = () => {
  nextTick(() => {
    readonly.value = true;
    inputRef.value.focus();
    setTimeout(() => {
      inputRef.value.focus();
      readonly.value = false;
    }, 0);
  });
};

const checkPlanQuantity = (rule: any, value: any, callback: any) => {
  // 可以为空
  if (!value) {
    return callback();
  }

  // 校验是否为数字
  const commonValue = Number(value);
  if (Number.isNaN(commonValue) && value) {
    return callback(`请输入数字`);
  }

  // 校验不能为复数
  if (commonValue < 0) {
    return callback(`请输入正确的数字`);
  }

  // 校验总数是否超过计划数量
  const { rowIndex, monthIndex, cellIndex } = ruleForm;
  const computedTableData = deepCopy(toRaw(tableData.value)); // 操作的tabledata
  let sumPlanCount = 0;
  computedTableData[rowIndex].newOrders.map((item, itemIndex) => {
    item.newClasses.map((cellItem, newIndex) => {
      if (cellItem.planQuantity) {
        // 当前数据不添加
        if (monthIndex === itemIndex && cellIndex === newIndex) {
        } else {
          sumPlanCount += cellItem.planQuantity * 1;
        }
      }
    });
  });
  if (sumPlanCount + value * 1 > computedTableData[rowIndex].planQuantity * 1) {
    const maxCount =
      computedTableData[rowIndex].planQuantity * 1 - sumPlanCount * 1;
    console.log(
      maxCount,
      sumPlanCount + value * 1,
      computedTableData[rowIndex].planQuantity * 1
    );
    return callback(
      `不能超过${parseInt(maxCount)},当前行计划之和不能超过总计划数量`
    );
  }

  let elderData: any = deepCopy(toRaw(tableData.value));
  elderData[rowIndex].newOrders[monthIndex].newClasses[cellIndex].planQuantity =
    value;
  elderData[rowIndex + 1].newOrders[monthIndex].newClasses[
    cellIndex
  ].planQuantity = value;

  const result = resolveSaveData(elderData);

  calculateLoadRate(result)
    .then((res: any) => {
      console.log("res", res, res[monthIndex * 3 + cellIndex]);
      const currentLoad = res[monthIndex * 3 + cellIndex] ?? "";
      if (Number(currentLoad) > maxCapacityLoad.value / 100) {
        return callback(
          `负荷率不能超过${(maxCapacityLoad.value / 100).toFixed(
            2
          )}，当前负荷率为${currentLoad}请重新分配`
        );
      } else {
        return callback();
      }
    })
    .catch(() => {
      return callback();
    });
};

const rulesPlan = reactive({
  quantity: [{ validator: checkPlanQuantity, trigger: "blur" }],
});

function clearRuleForm() {
  ruleFormRef.value.clearValidate();
  dialogVisible.value = false;
  ruleForm.rowIndex = null;
  ruleForm.monthIndex = null;
  ruleForm.cellIndex = null;
  ruleForm.rowData = null;
  ruleForm.quantity = "";
  ruleForm.yelid = "";
  ruleForm.noYelid = "";
}

const ruleFormRef = ref();
const submitPlanQuantity = () => {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      const { rowIndex, monthIndex, cellIndex, rowData, quantity } = ruleForm;
      let elderData: any = deepCopy(toRaw(tableData.value));
      elderData[rowIndex].newOrders[monthIndex].newClasses[
        cellIndex
      ].planQuantity = quantity ?? 0;
      elderData[rowIndex + 1].newOrders[monthIndex].newClasses[
        cellIndex
      ].planQuantity = quantity ?? 0;

      const result = resolveSaveData(elderData);

      tableLoadRate.value = await calculateLoadRate(result);
      // 修改第一行
      tableData.value[rowIndex].newOrders[monthIndex].newClasses[
        cellIndex
      ].planQuantity = quantity;
      // 修改第二行
      tableData.value[rowIndex + 1].newOrders[monthIndex].newClasses[
        cellIndex
      ].planQuantity = quantity;
      computedUnPlanQuantity(rowIndex);
      clearRuleForm();
    } else {
      console.log("error submit!");
    }
  });
};

// 定义钢卷选择选择器弹窗参数
const materialsSelect = reactive<DialogOption>({
  allocationId: "",
  rowIndex: "",
  visible: false,
  eldIds: [],
  data: [],
  planQuantity: "",
});
interface SelectOption {
  id?: string;
}
const materialsOptions: any = ref([]); // 选择器 list

// 接收钢卷选择器回调参数
// function getMaterialsSelect(options: any, index) {
//   // 新建的计划，可以修改
//   if (tableData.value[index].planState == 0) {
//     materialsOptions.value = options;
//     console.log("接收钢卷选择器回调", options);
//     tableData.value[index].requestPlanList = options;
//     tableData.value[index + 1].requestPlanList = options;

//     if (options.length) {
//       let sumQuantity = 0;
//       options.map((item) => {
//         sumQuantity += item.theoryQuantity ?? 0;
//       });
//       tableData.value[index].planQuantity = sumQuantity;
//       tableData.value[index + 1].planQuantity = sumQuantity;
//       tableData.value[index].requestPlanList = options;
//       tableData.value[index + 1].requestPlanList = options;
//     } else {
//       tableData.value[index].planQuantity = "";
//       tableData.value[index + 1].planQuantity = "";
//       tableData.value[index].requestPlanList = [];
//       tableData.value[index + 1].requestPlanList = [];
//     }
//   }
// }

// 接收钢卷选择器回调参数
function getMaterialsSelect(options: any, index, Allkg, AllNum) {
  // 新建的计划，可以修改
  // 没勾选时触发

  console.log("2222222", ids.value);

  if (ids.value.length == 0) {
    materialsOptions.value = options;
    console.log("接收钢卷选择器回调", options);
    tableData.value[index].defaultRequestPlanList = options;
    tableData.value[index + 1].defaultRequestPlanList = options;

    if (options.length) {
      let sumQuantity = 0;
      options.map((item) => {
        sumQuantity += item.theoryQuantity ?? 0;
      });
      tableData.value[index].defaultPlanQuantity = AllNum;
      tableData.value[index + 1].defaultPlanQuantity = AllNum;
      tableData.value[index].defaultRequestPlanList = options;
      tableData.value[index + 1].defaultRequestPlanList = options;
      // 理论 重量
      tableData.value[index].theoryQuantity = AllNum;
      tableData.value[index + 1].theoryQuantity = AllNum;
      tableData.value[index].materialWeight = Allkg;
      tableData.value[index + 1].materialWeight = Allkg;

      console.log(
        "理论 重量",
        tableData.value[index],
        tableData.value[index + 1]
      );
    } else {
      tableData.value[index].defaultPlanQuantity = "";
      tableData.value[index + 1].defaultPlanQuantity = "";
      tableData.value[index].defaultRequestPlanList = [];
      tableData.value[index + 1].defaultRequestPlanList = [];
    }
    sureRequstPlan(index);
  } else if (ids.value.length == 1) {
    // 勾选一个时

    materialsOptions.value = options;
    console.log("接收钢卷选择器回调", options);
    tableData.value[index].defaultRequestPlanList = options;
    tableData.value[index + 1].defaultRequestPlanList = options;

    if (options.length) {
      let sumQuantity = 0;
      options.map((item) => {
        sumQuantity += item.theoryQuantity ?? 0;
      });
      tableData.value[index].defaultPlanQuantity = AllNum;
      tableData.value[index + 1].defaultPlanQuantity = AllNum;
      tableData.value[index].defaultRequestPlanList = options;
      tableData.value[index + 1].defaultRequestPlanList = options;
      // 理论 重量
      tableData.value[index].theoryQuantity = AllNum;
      tableData.value[index + 1].theoryQuantity = AllNum;
      tableData.value[index].materialWeight = Allkg;
      tableData.value[index + 1].materialWeight = Allkg;
      console.log(
        "勾一个理论 重量",
        tableData.value[index],
        tableData.value[index + 1]
      );
    } else {
      tableData.value[index].defaultPlanQuantity = "";
      tableData.value[index + 1].defaultPlanQuantity = "";
      tableData.value[index].defaultRequestPlanList = [];
      tableData.value[index + 1].defaultRequestPlanList = [];
    }
    submits();
  } else {
    // 勾选多个时
    let mm = Allkg;
    let bb = AllNum;

    let sumQuantity = 0;
    options.map((item) => {
      sumQuantity += item.theoryQuantity ?? 0;
    });
    console.log("选中的计划", MaterialsList.value);

    // 先把 理论 重量 卷料数量为0
    tableData.value.forEach((item, index) => {
      MaterialsList.value.forEach((item2, index2) => {
        if (item2.row.id == item.id && index == item2.rowIndex) {
          item.defaultRequestPlanList = options;
          if (options.length) {
            item.defaultPlanQuantity = 0;
            item.defaultRequestPlanList = options;
            item.theoryQuantity = 0;
            item.materialWeight = 0;
            console.log("勾多个个理论 重量", item);
          } else {
            item.defaultPlanQuantity = "";
            item.defaultRequestPlanList = [];
            // 理论 重量
            item.theoryQuantity = "";
            item.materialWeight = "";
          }
        }
      });
    });

    let allNumber = MaterialsList.value.length / 2;
    tableData.value.forEach((item, index) => {
      MaterialsList.value.forEach((item2, index2) => {
        if (
          item2.row.id == item.id &&
          index == item2.rowIndex &&
          MaterialsList.value[index2 + 1]?.row?.id == item.id
        ) {
          if (MaterialsList.value.length - 2 == index2) {
            console.log("jinlai");
            console.log(Math.floor(mm / allNumber) + (mm % allNumber));
            tableData.value[index].materialWeight =
              Math.floor(mm / allNumber) + (mm % allNumber);
            tableData.value[index + 1].materialWeight =
              Math.floor(mm / allNumber) + (mm % allNumber);
          } else {
            tableData.value[index].materialWeight = Math.floor(mm / allNumber);
            tableData.value[index + 1].materialWeight = Math.floor(
              mm / allNumber
            );
          }
        }
      });
    });

    tableData.value.forEach((item, index) => {
      MaterialsList.value.forEach((item2, index2) => {
        if (
          item2.row.id == item.id &&
          index == item2.rowIndex &&
          MaterialsList.value[index2 + 1]?.row?.id == item.id
        ) {
          if (MaterialsList.value.length - 2 == index2) {
            console.log("jinlai");
            console.log(Math.floor(bb / allNumber) + (bb % allNumber));

            tableData.value[index].theoryQuantity =
              Math.floor(bb / allNumber) + (bb % allNumber);
            tableData.value[index + 1].theoryQuantity =
              Math.floor(bb / allNumber) + (bb % allNumber);
          } else {
            tableData.value[index].theoryQuantity = Math.floor(bb / allNumber);
            tableData.value[index + 1].theoryQuantity = Math.floor(
              bb / allNumber
            );
          }
        }
      });
    });
    submits();
    // 需求：选完的重量 按照已选的计划中的计划数量作比较
    // 1.选的卷料小于打勾的第一条计划数量 直接赋值 其他计划为0
    // 2.选的卷料不小于打勾的第一条计划数量 按照计划数量分配卷料 依次分配
    // 3.如果每条计划分配完还有卷料 把剩余卷料给最后一个计划

    // 重量计算
    // let nn = true;
    // tableData.value.forEach((item, index) => {
    //   MaterialsList.value.forEach((item2, index2) => {
    //     if (nn) {
    //       if (
    //         item2.row.id == item.id &&
    //         index == item2.rowIndex &&
    //         MaterialsList.value[index2 + 1]?.row?.id == item.id
    //       ) {
    //         if (tableData.value[index].planQuantity >= mm) {
    //           tableData.value[index].materialWeight = mm;
    //           tableData.value[index + 1].materialWeight = mm;
    //           nn = false;
    //           return;
    //         } else if (MaterialsList.value.length - 2 == index2) {
    //           tableData.value[index].materialWeight = mm;
    //           tableData.value[index + 1].materialWeight = mm;
    //           nn = false;
    //           return;
    //         } else {
    //           mm = mm - tableData.value[index].planQuantity;
    //           tableData.value[index].materialWeight =
    //             tableData.value[index].planQuantity;
    //           tableData.value[index + 1].materialWeight =
    //             tableData.value[index].planQuantity;
    //         }
    //       } else {
    //         return;
    //       }
    //     }
    //   });
    // });
    // 理论计算 卷料
    // let nn1 = true;
    // tableData.value.forEach((item, index) => {
    //   MaterialsList.value.forEach((item2, index2) => {
    //     if (nn1) {
    //       if (
    //         item2.row.id == item.id &&
    //         index == item2.rowIndex &&
    //         MaterialsList.value[index2 + 1]?.row?.id == item.id
    //       ) {
    //         if (tableData.value[index].planQuantity >= bb) {
    //           tableData.value[index].theoryQuantity = bb;
    //           tableData.value[index + 1].theoryQuantity = bb;
    //           tableData.value[index].defaultPlanQuantity = bb;
    //           tableData.value[index + 1].defaultPlanQuantity = bb;
    //           nn1 = false;
    //           return;
    //         } else if (MaterialsList.value.length - 2 == index2) {
    //           tableData.value[index].theoryQuantity = bb;
    //           tableData.value[index + 1].theoryQuantity = bb;
    //           tableData.value[index].defaultPlanQuantity = bb;
    //           tableData.value[index + 1].defaultPlanQuantity = bb;
    //           nn1 = false;
    //           return;
    //         } else {
    //           bb = bb - tableData.value[index].planQuantity;
    //           tableData.value[index].theoryQuantity =
    //             tableData.value[index].planQuantity;
    //           tableData.value[index + 1].theoryQuantity =
    //             tableData.value[index].planQuantity;
    //           tableData.value[index].defaultPlanQuantity =
    //             tableData.value[index].planQuantity;
    //           tableData.value[index + 1].defaultPlanQuantity =
    //             tableData.value[index].planQuantity;
    //         }
    //       } else {
    //         return;
    //       }
    //     }
    //   });
    // });
  }

  // tableData.value.forEach((item2, index2) => {
  //   if (item2.defaultRequestPlanList.length > 0) {
  //     item2.requestPlanList = item2.defaultRequestPlanList;
  //   }
  // });
  // console.log("卷料确认", tableData.value);

  // const postUrl = resolveUrl("保存");
  // // 最后提交
  // lastMaterialsSubmit(tableData.value, "保存", postUrl);
}
// 批量确认
const submits = () => {
  if (!isSearched.value) {
    ElMessage.error("请切换到对应排产后，再添加");
    return;
  } else {
    if (ids.value.length > 0) {
      const noNumber = ref([]);
      const no = ref([]);
      ids.value.forEach((item, index) => {
        tableData.value.forEach((item2, index2) => {
          if (item2.id == item) {
            if (item2.defaultPlanQuantity == 0) {
              noNumber.value.push(item2);
            }
          }
        });
      });
      // if (noNumber.value.length > 0) {
      //   ElMessage.error("请选择卷料");
      //   return;
      // }
      // // 判断钢卷数量是否小于计划数量
      // ids.value.forEach((item, index) => {
      //   tableData.value.forEach((item2, index2) => {
      //     if (item2.id == item) {
      //       if (item2.defaultPlanQuantity < item2.planQuantity) {
      //         no.value.push(item2);
      //       }
      //     }
      //   });
      // });

      // if (no.value.length > 0) {
      //   ElMessage.error("卷料数量不能小于计划数量");
      //   return;
      // }

      // ElMessageBox.confirm("确定批量确认?", "提示", {
      //   confirmButtonText: "确认",
      //   cancelButtonText: "取消",
      //   type: "warning",
      // }).then(() => {
      ids.value.forEach((item, index) => {
        tableData.value.forEach((item2, index2) => {
          if (item2.id == item) {
            // item2.planQuantity = item2.defaultPlanQuantity;
            item2.requestPlanList = item2.defaultRequestPlanList;
          }
        });
      });
      console.log("tableData.value", tableData.value);

      const postUrl = resolveUrl("保存");
      // 最后提交
      lastMaterialsSubmit(tableData.value, "保存", postUrl);
      // });
    } else {
      ElMessage.error("请选择数据");
      return;
    }
  }
};

// // // 确认卷料
// function sureRequstPlan(index: number) {
//   if (tableData.value[index].defaultPlanQuantity == 0) {
//     ElMessage.error("请选择卷料");
//     return;
//   }
//   if (
//     tableData.value[index].defaultPlanQuantity <
//     tableData.value[index].planQuantity
//   ) {
//     ElMessage.error("卷料数量不能小于计划数量");
//     return;
//   }
//   const arr = [index, index + 1];
//   ElMessageBox.confirm(
//     "确认后卷料产量将替换计划数量，是否继续该操作?",
//     "提示",
//     {
//       confirmButtonText: "确认",
//       cancelButtonText: "取消",
//       type: "warning",
//     }
//   ).then(() => {
//     arr.map((item) => {
//       tableData.value[item].requestPlanList =
//         tableData.value[item].defaultRequestPlanList;
//       tableData.value[item].planQuantity =
//         tableData.value[item].defaultPlanQuantity;
//     });

//     const postUrl = resolveUrl("保存");
//     lastMaterialsSubmit(tableData.value, "保存", postUrl);
//   });
// }

/** 定义form信息 */
const rules = reactive({
  routingAllocationId: [
    { required: true, message: "请选择", trigger: "change" },
  ],
  planQuantity: [
    { required: true, message: "请输入", trigger: "blur" },
    {
      validator: validateNumber,
      trigger: "blur",
    },
  ],
  planBatch: [
    { required: true, message: "请输入", trigger: "blur" },
    {
      validator: validateNumber,
      trigger: "blur",
    },
  ],
});

const formRef = ref(ElForm);

const currentYear = new Date().getFullYear(); // 当前年份
const currentWeek = getWeekNumber(new Date()); // 当前第几周
const classCount = 3; // 固定三个班次

// 表格日期对象 [{date: '05/06', week: 1}, {date: '05/07', week: 2}...]
const currentWeekDetail: any = ref<WeekDetailType[]>([]);
const freeDayDetail: any = ref([]);

// 初始化搜索参数数据
const searchParamInit = {
  planWeek: currentWeek,
  planYear: currentYear,
  workCenterId: "",
  plantId: "",
  planBatch: "",
};

const searchedParams = ref();

const isSearched = ref(false); // 是否已搜索
let tableData: any = ref([]); // 表格数据

function validIsSearched(type) {
  if (!isSearched.value) {
    ElMessage.error("请切换到对应排产后，再操作");
    return;
  } else {
    if (type === "保存") {
      savePlan(tableData.value, "保存");
    } else if (type === "下达") {
      issueOrders();
    } else if (type === "关闭") {
      closePlan();
    } else if (type === "删除") {
      deletePlan();
    }
  }
}

const ids = ref<string[]>([]);
const dataTableRef = ref();
const checkedrow = ref([]);

/** 行复选框选中  */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id || item.key);
  // const result = resolveSaveData(tableData.value);

  // selection.forEach((item) => {
  //   console.log(item.isSelected);
  // });

  // ids.value = selection.map((item: any) => item.id || item.key);
  // checkedrow.value = selection;
  // const selectedIds = new Set(selection.map((row) => row.rollMaterialId));

  // // 更新所有行的选中状态
  // tableData.value.forEach((row) => {
  //   if (selectedIds.has(row.rollMaterialId)) {
  //     row.isSelected = true;
  //   } else {
  //     row.isSelected = false;
  //   }
  // });

  // // 如果有新的行被选中，找到所有具有相同ID的行并设置它们的选中状态
  // selection.forEach((selectedRow) => {
  //   const sameIdRows = tableData.value.filter(
  //     (row) => row.rollMaterialId === selectedRow.rollMaterialId
  //   );
  //   sameIdRows.forEach((row) => {
  //     if (!checkedrow.value.includes(row)) {
  //       dataTableRef.value.toggleRowSelection(row, true);
  //     }
  //   });
  // });
}
const handleSelect = (selection, row) => {
  console.log("handleSelect", selection, row);

  if (!selection.includes(row)) {
    // 如果是取消选中
    // 更新对应的行的选中状态
    row.isSelected = false;
    // 移除该行从已选中的行列表中
    checkedrow.value = checkedrow.value.filter(
      (selectedRow) => selectedRow !== row
    );

    // // 检查是否有其他行与当前行拥有相同的ID，如果有，则检查它们是否应该被取消选中
    // const sameIdRows = tableData.value.filter(
    //   (r) => r.rollMaterialId === row.rollMaterialId && r !== row
    // );
    // sameIdRows.forEach((sameRow) => {
    //   if (
    //     !this.selectedRows.some(
    //       (selectedRow) => selectedRow.rollMaterialId === sameRow.rollMaterialId
    //     )
    //   ) {
    //     dataTableRef.value.toggleRowSelection(sameRow, false);
    //     sameRow.isSelected = false;
    //   }
    // });
  }
};
// 确认卷料
function sureRequstPlan(index: number) {
  // if (
  //   tableData.value[index].defaultPlanQuantity <
  //   tableData.value[index].planQuantity
  // ) {
  //   ElMessage.error("卷料数量不能小于计划数量");
  //   return;
  // }
  const arr = [index, index + 1];
  // ElMessageBox.confirm(
  //   "确认后卷料产量将替换计划数量，是否继续该操作?",
  //   "提示",
  //   {
  //     confirmButtonText: "确认",
  //     cancelButtonText: "取消",
  //     type: "warning",
  //   }
  // ).then(() => {
  //   arr.map((item) => {
  //     tableData.value[item].requestPlanList =
  //       tableData.value[item].defaultRequestPlanList;
  //     // tableData.value[item].planQuantity =
  //     //   tableData.value[item].defaultPlanQuantity;
  //   });
  //   console.log("tableData.value", tableData.value);

  // });

  arr.map((item) => {
    tableData.value[item].requestPlanList =
      tableData.value[item].defaultRequestPlanList;
    // tableData.value[item].planQuantity =
    //   tableData.value[item].defaultPlanQuantity;
  });
  const postUrl = resolveUrl("保存");
  lastMaterialsSubmit(tableData.value, "保存", postUrl);
}

// 最终物料操作接口
async function lastMaterialsSubmit(
  resultTableData: any,
  message: string,
  postUrl: string
) {
  const result = resolveSaveData(resultTableData);
  console.log(tableData.value);

  showLoading(message, true);
  const { data, code } = await request({
    url: postUrl,
    method: "post",
    data: result,
  }).finally(() => {
    showLoading(message, false);
  });
  if (code === 200) {
    ElMessage.success(`${message == "保存" ? "操作" : message}成功`);
  }
  search();
}

function cancelRequstPlan(index: number) {}

// 批量关闭
const closeLoading = ref(false);
function closePlan() {
  if (!ids.value.length) {
    ElMessage.warning("请选择数据");
    return;
  } else {
    ElMessageBox.confirm(
      "未下达数据不能关闭，请确认是否关闭所选数据?",
      "提示",
      {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }
    )
      .then(async () => {
        let listIds: any = ""; //第几行数据  不能关闭提示
        const resultTableData = deepCopy(toRaw(tableData.value));
        resultTableData.map((item: any, index: number) => {
          if (
            item.planState * 1 === 0 &&
            (ids.value.indexOf(item.id) !== -1 ||
              ids.value.indexOf(item?.key) !== -1)
          ) {
            if (index % 2 === 0) {
              const newLine = (index + 2) / 2;
              listIds += `第${newLine}行、`;
            }
          } else if (
            item.planState * 1 > 0 &&
            ids.value.indexOf(item.id) !== -1
          ) {
            item.planState = 5;
          }
        });
        if (listIds.length) {
          // 提示未下达过的计划，不能关闭
          listIds = listIds.slice(0, listIds.length - 1);
          ElMessage.error(`${listIds}未下达，不能关闭，请重新选择`);
          return;
        }
        // 走关闭成功状态更新接口保存计划
        savePlan(resultTableData, "关闭");
        // ElMessage.success("批量关闭成功");
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消关闭",
        });
      });
  }
}

// 批量删除
const deleteLoading = ref(false);
function deletePlan() {
  const idsSave = deepCopy(toRaw(ids.value));
  if (!ids.value.length) {
    ElMessage.warning("请选择数据");
    return;
  } else {
    ElMessageBox.confirm("请确认是否删除所选数据?", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        const tableDatas: any = [];
        // const resultTableData = deepCopy(toRaw(tableData.value));
        tableData.value.map((item: any) => {
          if (ids.value.indexOf(item?.key) !== -1 && item.id === "") {
          } else if (ids.value.indexOf(item.id) !== -1) {
            item.deleted = 1;
            tableDatas.push(item);
          } else {
            tableDatas.push(item);
          }
        });
        tableData.value = tableDatas;
        getSelectedAllocationIds();
        // 走删除状态更新接口保存计划
        if (tableData.value.length) {
          // 走删除状态更新接口保存计划
          savePlan(tableData.value, "删除", idsSave);
        }
        // ElMessage.success("批量下达成功");
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除",
        });
      });
  }
}

// 批量下达
const issueLoading = ref(false);
function issueOrders() {
  // console.log("ids", ids);
  if (!ids.value.length) {
    ElMessage.warning("请选择数据");
    return;
  } else {
    ElMessageBox.confirm("请确认是否下达所选数据?", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        let listIds: any = ""; //第几行数据  不能下达提示
        const resultTableData = deepCopy(toRaw(tableData.value));
        resultTableData.map((item: any, index: number) => {
          if (item.planState * 1 > 0 && ids.value.indexOf(item.id) !== -1) {
            if (index % 2 === 0) {
              const newLine = (index + 2) / 2;
              listIds += `第${newLine}行、`;
            }
          } else if (
            item.planState * 1 === 0 &&
            ids.value.indexOf(item.id) !== -1
          ) {
            item.planState = 2;
          } else if (
            item.planState * 1 === 0 &&
            ids.value.indexOf(item.key) !== -1 &&
            !item.id
          ) {
            item.planState = 2;
          }
        });
        if (listIds.length) {
          // 提示已下达过的计划，不能重复下达
          listIds = listIds.slice(0, listIds.length - 1);
          ElMessage.error(`${listIds}不能重新下达，请重新选择`);
          return;
        }
        // 走下达状态更新接口保存计划
        savePlan(resultTableData, "下达");
        // ElMessage.success("批量下达成功");
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消下达",
        });
      });
  }
}

const currentYield = ref(); // 当前班产
const searchParam = ref<SearchType>({ ...searchParamInit });
const loading = ref(false);
const oneLineTable = ref();
const elderData: any = ref();

const tableLoadRate: any = ref([]);
// 搜索
const search = async () => {
  loading.value = true;
  tableData.value = [];
  const { data } = await request({
    url: "/mes/mesworkorder/queryByWeek",
    method: "post",
    data: toRaw(searchParam.value),
  }).catch(() => {
    loading.value = false;
  });

  // 获取接口原数据
  elderData.value = deepCopy(data ?? []);

  searchedCompanyId.value = toRaw(companyId.value);

  searchedParams.value = toRaw(searchParam.value);
  isSearched.value = true;
  // 获取班产量
  // currentYield.value = await getCurrentYield(searchParam.value.workCenterId);

  // 最新表格时间处理
  currentWeekDetail.value = getCurrentWeekDetail(
    searchParam.value,
    freeDetail.value
  );
  // 最新休息日数据
  const arr: any = [];
  currentWeekDetail.value.map((e: any) => {
    if (e.isFree === true) {
      arr.push(
        searchedParams.value.planYear + "-" + e.date.split("/").join("-")
      );
    }
  });
  freeDayDetail.value = arr;

  // 一行表格初始化
  // oneLineTable.value = getOneLineDate(
  //   currentWeekDetail.value,
  //   toRaw(searchParam.value.planYear)
  // );
  // 表格数据处理 mockData
  // tableData.value = resolveSearchData(
  //   data,
  //   currentWeekDetail.value,
  //   toRaw(searchParam.value.planYear)
  // );
  tableLoadRate.value = await calculateLoadRate(data);

  tableData.value = resolveSearchData(
    data,
    currentWeekDetail.value,
    toRaw(searchParam.value.planYear)
  );

  console.log("tableData.value", tableData.value);

  // 计算当前未排数量
  data?.forEach((item, index) => {
    const selfIndex = (index + 1) * 2 - 2;
    computedUnPlanQuantity(selfIndex);
  });

  loading.value = false;

  // 获取当前页面已选的物料号ids
  getSelectedAllocationIds();
};

const reset = () => {
  searchParam.value = { ...searchParamInit };
  searchParam.value.plantId = plantId.value;
  if (plantId.value) {
    selectPlant(plantId.value);
  } else {
    workShopList.value = [];
  }
  tableData.value = [];
  workCenterDetail.value = {};
  isSearched.value = false;
};

// 获取周列表
const monthOptions = ref<OptionType[]>();

// 获取年度列表
const yearOptions = ref();

// 年度变更重新获取月份
function selectYear(e: number) {
  if (e) {
    searchParam.value = { ...searchParam.value, planWeek: "" };
    monthOptions.value = getMonthList(e);
  } else {
    monthOptions.value = [];
    searchParam.value = { ...searchParam.value, planWeek: "" };
  }
}

// 工厂编号列表
const plantList: any = ref([]);
// 工作中心列表
const workShopList: any = ref([]);

// 当前选中的工厂
const companyId = ref("");
const searchedCompanyId = ref("");
async function selectPlant(e: string) {
  workShopList.value = (await getFirstWorkShopList(e)) ?? [];
  searchParam.value = { ...searchParam.value, workCenterId: "" };
  companyId.value =
    plantList.value.find((item) => item.id === e)?.parentId ?? "";
}
const plantId = ref("");
onMounted(async () => {
  // 获取工厂列表
  await getPlantList().then((res) => {
    plantList.value = res;
    searchParam.value.plantId = plantList.value[0].id;
    selectPlant(plantList.value[0].id);
  });
  // 获取年列表
  yearOptions.value = await getYearList();
  // 获取周搜索列表
  monthOptions.value = getMonthList(currentYear);
  // 表格日期对象 [{date: '05/06', week: 1}, {date: '05/07', week: 2}...]
  currentWeekDetail.value = getCurrentWeekDetail(searchParam.value);
});

// 导入周数据
const weekImportRef = ref();
async function weekImportApiFunc(formdata: FormData, config: any) {
  await request.post("/mdm/mdmmatgroup/upload", formdata, config);
}
const weekBatchImport = () => {
  if (!isSearched.value) {
    ElMessage.error("请切换到对应排产后，再操作");
  } else {
    let params = {
      title: "导入周计划",
      tempApi: "/mdm/mdmmatgroup/exportTemp", //模板下载
      importApi: weekImportApiFunc, // 导入
      getTableList: search, // 成功后回调
    };
    weekImportRef.value.acceptParams(params);
  }
};

const downLoadExcel = () => {
  if (!isSearched.value) {
    ElMessage.error("请切换到对应排产后，再操作");
  } else {
    useDownload(
      "/mes/mesworkorder/export",
      "计划排程-周排产(落料)",
      searchParam.value,
      true,
      ".xls"
    );
  }
};

// 导入日数据
const dailyImportRef = ref();
async function dailyImportApiFunc(formdata: FormData, config: any) {
  await request.post("/mdm/mdmmatgroup/upload", formdata, config);
}
const dailyBatchImport = () => {
  if (!isSearched.value) {
    ElMessage.error("请切换到对应排产后，再操作");
  } else {
    let params = {
      title: "导入日计划",
      tempApi: "/mdm/mdmmatgroup/exportTemp", //模板下载
      importApi: dailyImportApiFunc, // 导入
      getTableList: search, // 成功后回调
    };
    dailyImportRef.value.acceptParams(params);
  }
};

// 工作中心详情
const workCenterDetail = ref();
// 最大负荷率
const maxCapacityLoad = ref();
// 休息日设定信息
const freeDetail = ref();
// 物料列表
const routingAllocationIdList = ref();
// 日班次
const dateshiftCount = ref();

async function startSearch() {
  tableData.value = [];
  isSearched.value = false;
  if (!searchParam.value.plantId) {
    ElMessage.error("请选择工厂");
    return;
  } else if (!searchParam.value.workCenterId) {
    ElMessage.error("请选择工作中心");
    return;
  } else if (!searchParam.value.planYear) {
    ElMessage.error("请选择年份");
    return;
  } else if (!searchParam.value.planWeek) {
    ElMessage.error("请选择工作周");
    return;
  } else {
    // 工作中心详情
    const { workCenterId, planYear, plantId } = searchParam.value;
    workCenterDetail.value = await getWorkCenterDetail(workCenterId);
    dateshiftCount.value = await getDateshiftCount(searchParam.value);
    if (dateshiftCount.value.length === 0) {
      ElMessage.error("请先设置排班");
      return;
    }
    // 获取最大负荷率
    maxCapacityLoad.value = workCenterDetail.value.maxCapacityLoad;
    // 获取休息日数据
    freeDetail.value = await getFreeDetails(plantId, planYear);
    // 获取物料下拉列表
    routingAllocationIdList.value = await getMaterialList(
      searchParam.value.plantId,
      searchParam.value.workCenterId
    );
    search();
  }
}

// 当前页面已选的物料号ids
const selectedRoutingAllocationIds: any = ref([]);
// 选择物料号
async function changeRoutingAllocation(data: string, index: number) {
  const indexArr = [index, index + 1];
  if (data) {
    const current = routingAllocationIdList.value.filter((e) => e.id === data);
    const res: any = await getCurrentYield(
      searchParam.value.plantId,
      current[0]?.routingId
    );
    indexArr.map((item) => {
      tableData.value[item].requestPlanList = [];
      tableData.value[item].routingAllocationId = current[0]?.id || "";
      tableData.value[item].routingId = current[0]?.routingId || "";
      tableData.value[item].actualYield = res.actualYield;
      tableData.value[item].yield = res.maxYield;
    });
  } else {
    indexArr.map((item) => {
      tableData.value[item].requestPlanList = [];
      tableData.value[item].routingAllocationId = "";
      tableData.value[item].routingId = "";
      tableData.value[item].yield = "";
    });
  }
  getSelectedAllocationIds();
}

// 获取当前页面已选的物料号ids
function getSelectedAllocationIds() {
  const selectArr: any[] = [];
  tableData.value.map((item, index) => {
    if (item.planState * 1 !== 5) {
      selectArr.push(item.routingAllocationId);
    }
  });
  selectedRoutingAllocationIds.value = Array.from(new Set(selectArr));
}

function changePlanQuantity(e, index) {
  // 计算未排数量  待计算  用计划减去每隔的和
  computedUnPlanQuantity(index);
}

// 接口自动分配
async function rulePlanQuantity(e, index) {
  formRef.value
    .validateField(index + ".planQuantity")
    .then(async (res) => {
      const resultTableData = deepCopy(toRaw(tableData.value));
      resultTableData[index].autoAllocation = 1;
      resultTableData[index + 1].autoAllocation = 1;
      const result = resolveSaveData(resultTableData);
      tableData.value[index].autoAllocationLoading = true;
      const { data, code } = await request({
        url: "/mes/mesworkorder/autoAllocation",
        method: "post",
        data: result,
      }).finally(() => {
        tableData.value[index].autoAllocationLoading = true;
      });
      if (code === 200) {
        ElMessage.success(`自动分配成功`);
        tableLoadRate.value = await calculateLoadRate(data);
        tableData.value = resolveSearchData(
          data,
          currentWeekDetail.value,
          toRaw(searchParam.value.planYear)
        );

        // 计算当前未排数量
        data?.forEach((item, index) => {
          const selfIndex = (index + 1) * 2 - 2;
          computedUnPlanQuantity(selfIndex);
        });

        // 获取当前页面已选的物料号ids
        getSelectedAllocationIds();
      }
    })
    .catch(() => {});
}

// 校验当前班次是否存在 planDate计划日期、cellIndex当前班次
function isWorkingCrew(planDates: string, cellIndex: number) {
  let result = false;
  const current = dateshiftCount.value.filter((e: any) =>
    e.planDate.split("/").join("-").includes(planDates)
  );
  if (current && current[0] && current[0].shiftCount > cellIndex) {
    result = true;
  }
  return result;
}

// 计算未排产数量
function computedUnPlanQuantity(index) {
  // console.log("开始计算", index);
  const indexArr = [index, index + 1];
  let totalQuantity = 0;
  tableData.value[index].newOrders.map((orderItem) => {
    orderItem.newClasses.map((count) => {
      totalQuantity += count.planQuantity * 1;
    });
  });
  // 修改第一行和第二行的值
  indexArr.map((tableIndex) => {
    const computedNumber = Math.floor(
      tableData.value[tableIndex].planQuantity - totalQuantity
    );
    const number = computedNumber < 0 ? 0 : computedNumber;
    tableData.value[tableIndex].unPlanQuantity = number;
  });
}

// 添加卷料
const MaterialsList = ref([]);
const MaterialsList1 = ref([]);
const addPlan = async () => {
  if (!isSearched.value) {
    ElMessage.error("请切换到对应排产后，再添加");
    return;
  } else {
    console.log(ids.value);
    if (ids.value.length > 0) {
      MaterialsList.value = [];
      MaterialsList1.value = [];
      const result = resolveSaveData(tableData.value);
      result.forEach((item, index) => {
        ids.value.forEach((item1) => {
          if (item.id == item1) {
            console.log(item);

            MaterialsList1.value.push(item.routingAllocationId);
          }
        });
      });
      console.log("66", MaterialsList1.value);

      tableData.value.forEach((item, index) => {
        ids.value.forEach((item1) => {
          if (item.id == item1) {
            console.log(item);

            MaterialsList.value.push({ row: item, rowIndex: index });
          }
        });
      });
      let a = await isMainMaterial(MaterialsList1.value);
      console.log(a);
      console.log("99999", a);

      let hh = MaterialsList.value.every(
        (item) =>
          item.row.rollMaterialId === MaterialsList.value[0].row.rollMaterialId
      );

      if (a) {
        const { row, rowIndex } = MaterialsList.value[0];

        console.log(2222, row, rowIndex);

        selectIdsMaterials(row, rowIndex);
      } else {
        ElMessage.error("请选择相同物料或者未设置卷料！");
        return;
      }
      console.log(hh);
      console.log("MaterialsList", MaterialsList);
      // MaterialsList.value.forEach((item, index) => {
      //   if(item.item.materialId)
      // })
    } else {
      ElMessage.error("请选择至少一条数据");
      return;
    }
  }
};

// 验证物料号是否相同
function isMainMaterial(result) {
  return new Promise(async (resolve, reject) => {
    const { data } = await request({
      url: "/mes/mesroutingbom/isMainMaterial",
      method: "post",
      data: result,
    });
    resolve(data);
  });
}

// 打勾选择钢卷
const rowIndexs = ref("");
function selectIdsMaterials(row, rowIndex) {
  formRef.value
    .validateField(rowIndex + ".routingAllocationId")
    .then(async (res) => {
      const { data } = await request({
        url: `/mes/mesroutingbom/listMainMaterial/${row.routingAllocationId}`,
        method: "GET",
        data: {},
      });
      if (!data || !data.length || data.length === 0) {
        ElMessage.error("请给当前工序设置卷料");
      } else {
        materialsSelect.eldIds = [];
        if (row.id) {
          const current = tableData.value.filter(
            (item: any) => item.id === row.id
          );
          if (current && current.length) {
            let list: string[] = [];
            current[0].defaultRequestPlanList.map((self: any) => {
              list.push(self.materialBarcode);
            });
            // materialsSelect.eldIds = list || [];
            // 多选时清空id
            if (ids.value.length == 1) {
              materialsSelect.eldIds = list || [];
            } else {
              materialsSelect.eldIds = [];
            }
          }
        }

        // 多选时清空勾选的对号
        if (ids.value.length == 1) {
          materialsSelect.data = row.defaultRequestPlanList || [];
        } else {
          materialsSelect.data = [];
        }

        console.log(materialsSelect.eldIds, materialsSelect.data);
        console.log("data", materialsSelect);

        // materialsSelect.data = row.defaultRequestPlanList || [];
        materialsSelect.planQuantity = 0;
        const result = resolveSaveData(tableData.value);
        result.forEach((item, index) => {
          ids.value.forEach((item1) => {
            if (item.id == item1) {
              console.log("item", item);
              materialsSelect.planQuantity += item.planQuantity;
            }
          });
        });
        materialsSelect.allocationId = row.routingAllocationId;
        materialsSelect.rowIndex = rowIndex;
        materialsSelect.readonly = row.planState * 1 > 0 ? true : false;
        materialsSelect.visible = true;
      }
    })
    .catch(() => {});
}

// 钢卷选择
function selectMaterials(row, rowIndex) {
  console.log("row", row, rowIndex);

  formRef.value
    .validateField(rowIndex + ".routingAllocationId")
    .then(async (res) => {
      const { data } = await request({
        url: `/mes/mesroutingbom/listMainMaterial/${row.routingAllocationId}`,
        method: "GET",
        data: {},
      });
      console.log("data", data);
      console.log("routingAllocationId", row.routingAllocationId);
      if (!data || !data.length || data.length === 0) {
        ElMessage.error("请给当前工序设置卷料");
      } else {
        materialsSelect.eldIds = [];
        if (row.id) {
          const current = tableData.value.filter(
            (item: any) => item.id === row.id
          );
          if (current && current.length) {
            console.log("current", current);

            let list: string[] = [];
            current[0].defaultRequestPlanList.map((self: any) => {
              list.push(self.materialBarcode);
            });
            materialsSelect.eldIds = list || [];
          }
        }
        console.log("materialsSelect.eldIds", row.defaultRequestPlanList);

        materialsSelect.planQuantity = row.planQuantity || "";
        materialsSelect.data = row.defaultRequestPlanList || [];
        materialsSelect.allocationId = row.routingAllocationId;
        materialsSelect.rowIndex = rowIndex;
        materialsSelect.readonly = false;
        materialsSelect.visible = true;
      }
    })
    .catch(() => {});
}

const isOverLoadRate = ref(false);

watch(
  () => tableLoadRate.value,
  (newValue) => {
    const overArrs = newValue?.filter(
      (item) => Number(item) > maxCapacityLoad.value / 100
    );
    isOverLoadRate.value = overArrs && overArrs?.length ? true : false;
  },
  {
    deep: true,
  }
);
const saveLoading = ref(false);
// function bornDescriptionCode(id, list) {}
function savePlan(resultTableData: any, message: "", idsSave?: any) {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      const postUrl = resolveUrl(message);
      const idList = message == "删除" ? idsSave : ids.value;

      // 添加selected被选中标识
      resultTableData.forEach((item: any, index: number) => {
        item.selected = 0;
        if (idList.indexOf(item.id) !== -1 || idList.indexOf(item.key) !== -1) {
          item.selected = 1;
        } else {
          item.selected = 0;
        }
      });

      // 已下达数据校验,获取下达时被勾选的数据,判断钢卷和计划数量是否为空
      if (message === "下达") {
        for (let i = 0; i < resultTableData.length; i++) {
          if (
            i % 2 === 0 &&
            resultTableData[i].selected == 1 &&
            resultTableData[i].planState != 5
          ) {
            // if (resultTableData[i].requestPlanList.length === 0) {
            //   ElMessage.error("下达的数据，卷料不能为空");
            //   return;
            // }
            let noPlanQuantity = true;
            for (let j = 0; j < resultTableData[i].newOrders.length; j++) {
              const newClasses = resultTableData[i].newOrders[j].newClasses;
              for (let b = 0; b < newClasses.length; b++) {
                if (
                  newClasses[b].planQuantity &&
                  newClasses[b].planQuantity != 0
                ) {
                  noPlanQuantity = false;
                  break;
                }
              }
            }

            if (noPlanQuantity) {
              ElMessage.error("下达的数据，本周的计划分配数量不能全为空");
              return;
            }
          }
        }
      }

      // 排查当前是否有超负荷率的情况
      // if (isOverLoadRate.value) {
      //   ElMessageBox.confirm(
      //     "当前生产计划有负荷率超标情况，请确定是否继续操作",
      //     "提示",
      //     {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       type: "warning",
      //     }
      //   )
      //     .then(() => {
      //       lastSubmit(resultTableData, message, postUrl);
      //     })
      //     .catch(() => {
      //       ElMessage({
      //         type: "info",
      //         message: "已取消",
      //       });
      //     });
      // } else {
      //   lastSubmit(resultTableData, message, postUrl);
      // }
      lastSubmit(resultTableData, message, postUrl);
    } else {
      ElMessage.warning("请完善基础信息");
    }
  });
}

// 最终操作接口
async function lastSubmit(
  resultTableData: any,
  message: string,
  postUrl: string
) {
  const result = resolveSaveData(resultTableData);
  showLoading(message, true);
  const { data, code } = await request({
    url: postUrl,
    method: "post",
    data: result,
  }).finally(() => {
    showLoading(message, false);
  });
  if (code === 200) {
    ElMessage.success(`${message == "保存" ? "操作" : message}成功`);
    search(); // 更新数据
  }
}

function resolveUrl(message) {
  let url = "";
  if (message == "保存") {
    url = "/mes/mesworkorder/saveWeekFirst";
  } else if (message == "下达") {
    url = "/mes/mesworkorder/issuePlan";
  } else if (message == "删除") {
    url = "/mes/mesworkorder/deletePlan";
  } else if (message == "关闭") {
    url = "/mes/mesworkorder/closePlan";
  } else {
    url = "/mes/mesworkorder/saveWeekFirst";
  }
  return url;
}

function showLoading(message, loading) {
  if (message == "保存") {
    saveLoading.value = loading;
  } else if (message == "下达") {
    issueLoading.value = loading;
  } else if (message == "删除") {
    deleteLoading.value = loading;
  } else if (message == "关闭") {
    closeLoading.value = loading;
  }
}

const open = (
  rowData: any,
  rowIndex: number,
  monthIndex: number,
  cellIndex: number,
  quantity: number | string
) => {
  if (rowData.planState * 1 !== 0) {
    return;
  }
  if (
    !isWorkingCrew(
      tableData.value[rowIndex].newOrders[monthIndex].planDate,
      cellIndex
    )
  ) {
    ElMessage.error("当前未设置班次");
    return;
  }
  if (!tableData.value[rowIndex].routingAllocationId) {
    ElMessage.error("请先选择工艺路线");
    return;
  }
  if (tableData.value[rowIndex].planQuantity * 1 === 0) {
    ElMessage.error("请先输入计划数量");
    return;
  }

  if (tableData.value[rowIndex].planState * 1 === 0) {
    dialogVisible.value = true;
    ruleForm.quantity = quantity ? quantity : tableData.value[rowIndex].yield;
    ruleForm.rowData = rowData;
    ruleForm.rowIndex = rowIndex;
    ruleForm.monthIndex = monthIndex;
    ruleForm.cellIndex = cellIndex;
    ruleForm.unPlanQuantity = tableData.value[rowIndex].unPlanQuantity;
    ruleForm.yelid = tableData.value[rowIndex].yield;
    ruleForm.noYelid = tableData.value[rowIndex].yield;

    if (ruleForm.quantity * 1 != 0) {
      ruleForm.quantity = ruleForm.quantity;
    } else {
      if (ruleForm.unPlanQuantity * 1 == 0) {
        ruleForm.quantity = 0;
      } else if (ruleForm.unPlanQuantity * 1 < ruleForm.noYelid * 1) {
        ruleForm.quantity = ruleForm.unPlanQuantity * 1;
      } else if (ruleForm.unPlanQuantity * 1 > ruleForm.noYelid * 1) {
        ruleForm.quantity = ruleForm.noYelid * 1;
      }
    }
    inputAutoFocus();
  }
};
</script>
<style lang="scss" scoped>
// @import url("@/views/mes/weeklyProducePlan/index.scss");
:deep(.planDialog .el-dialog__body) {
  padding: 20px 20px 0px;
}

.planTable {
  :deep(.el-table--default .el-table__cell) {
    padding: 12px 0 !important;
  }
  :deep(.el-table__empty-block) {
    min-height: 100px !important;
  }
  .el-button.is-link {
    margin: 10px 0 !important;
  }
}
</style>
